#!/usr/bin/env python
# Create: 2018/10/23
__author__ = '749B'
"""asyncio HTTP"""

import asyncio
from bs4 import BeautifulSoup


url_list = [
    'https://github.com/explore',
    'https://www.djangoproject.com/',
    'http://www.python-requests.org/en/master/',
    'https://jquery.com/',
    'https://getbootstrap.com/',
    'https://www.solarwinds.com/',
    'https://www.zabbix.com/',
    'http://open-falcon.org/',
    'https://www.python.org/',
    'http://www.jetbrains.com/',
]


@asyncio.coroutine
def fetch_async(host, url='/', port=80, ssl=False):
    reader, writer = yield from asyncio.open_connection(host, port, ssl=ssl)  # 建立tcp连接
    request_header_content = "GET %s HTTP/1.0\r\nHost: %s\r\n\r\n" % (url, host)  # 这个是GET请求
    request_header_content = request_header_content.encode('utf-8')  # 最终发送的是bytes类型
    writer.write(request_header_content)  # 发出请求
    yield from writer.drain()
    text = yield from reader.read()  # 接收到的当然也是bytes类型
    text = text.decode('utf-8')
    soup = BeautifulSoup(text, features='html.parser')
    title = soup.find('title')
    print(title)
    writer.close()


if __name__ == '__main__':
    from urllib.parse import urlparse
    tasks = []
    for url in url_list:
        url_parse = urlparse(url)
        if url_parse.scheme == "https":
            tasks.append(fetch_async(url_parse.netloc, url_parse.path, 443, True))
        else:
            tasks.append(fetch_async(url_parse.netloc, url_parse.path))
    loop = asyncio.get_event_loop()
    loop.run_until_complete(asyncio.gather(*tasks))
    loop.close()



